 /*
 * dbasesRoot.java
 *
 * Created on May 18, 2006, 2:08 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 * 
 * 13, Feb 2009
 *              the method processTreeHierarchy now prevents to process a null
 *              databaseObject.
 * 22, Feb 2011
 *              Method rump up; the global variable sDriver was renamed to sUrl in order to describe more accuretly its function as a
 *              connection argument.
 *              Begin with multiple sql driver support.
 * 10, Feb. 2014 Trivial modifications were made in order to make the coce compliant with Java 1.7
 */

package portfolio.dbHost;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;
import portfolio.dbHost.dataBases.DBase;
import portfolio.configMessage.configMessageEvent;
import portfolio.configMessage.configMessageFrame;
import portfolio.dbHost.dataBases.OdbcDBase;

import portfolio.mainScreen;

/**
 *
 * @author ACA
 */
public class OdbcHost extends Host //configMessageListener, MouseListener
{
    
    /**
     *
     * @param e 
     */
    public void okSelected     (configMessageEvent e)
    {
        configMessageFrame msgFrame = super.getMsgFrame();
        mainScreen mainFrame = super.getMainFrame ();
        try {
            String sCommand = msgFrame.getSQLStatementMnemonic();

            if        (sCommand.equals(Host.ACTION_LOGON_DB)) {
                Vector vConnectParams  = msgFrame.getSqlCreateParams();
                String sUrl            = vConnectParams.get(0).toString();
                String sHost           = vConnectParams.get(1).toString();
                String sDataSourceName = vConnectParams.get(2).toString();
                String sUser           = vConnectParams.get(3).toString();
                String sPassword       = vConnectParams.get(4).toString();
                super.setUrl (sUrl);
                super.setHost (sHost);
                super.setDataSourceName(sDataSourceName);
                super.setUser(sUser);
                super.setPassword(sPassword);
                super.setConnection (this.connectToDB (Host.DB_TYPE_ODBC, sUrl, sHost, sDataSourceName, sUser, sPassword));

                this.printConnectionMetadata();

                this.buildDBTree ();
                this.setDBTree (mainFrame.getDBasePanel ());
                mainFrame.displayMessage("Host.okSelected 81", Host.ACTION_LOGON_DB + " successful.");
            } else if (sCommand.equals(Host.ACTION_CREATE_DB)) {
                String sDBName = msgFrame.getFieldName();
                
                this.createDB (sDBName);
                this.buildDBTree();
                
                this.updateDBTree (); //mainFrame.getDBasePanel ());
                mainFrame.displayMessage("Host.okSelected 88", Host.ACTION_CREATE_DB + " " + sDBName + " successful.");
            } else {
                // 0j0 report error
            }
        } catch (Exception ex) {            
              mainFrame.displayMessage("Host.okSelected 91", ex.getMessage());
//0j0            connection = null;
        }
        msgFrame.dispose();
    }
    /**
     * Creates a new instance of DBase
     * @param mainFrameParam 
     * @throws java.lang.Exception 
     */
    public OdbcHost (mainScreen mainFrameParam) throws Exception
    {
        super (mainFrameParam);
        super.setDBtype(Host.DB_TYPE_ODBC);
    }
    /*
     *
     *
     */
    public static Connection ConnectToOdbcDB (String sUrlParam,
                                              String sDataSourceNameParam,
                                              String sUserParam,
                                              String sPasswdParam) throws Exception
    {

        Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver").newInstance ();
        String sLogonLine = sUrlParam + sDataSourceNameParam;
        return DriverManager.getConnection (sLogonLine, sUserParam, sPasswdParam);
    }
    /**
     *
     * @param iDbTypeParam
     * @param sUrl
     * @param sHost
     * @param sDataSourceName
     * @param sUser
     * @param sPasswd
     * @return
     * @throws Exception
     */
    public Connection connectToDB (int iDbTypeParam, // <----- & non static
                                          String sUrl,
                                          String sHost, 
                                          String sDataSourceName,
                                          String sUser,
                                          String sPasswd) throws Exception
    {
        Connection tmpConnection = null;
        
	try {
            if (iDbTypeParam == DB_TYPE_ODBC) {
                tmpConnection = ConnectToOdbcDB (sUrl, sDataSourceName, sUser, sPasswd);
            } else {
                throw new Exception ("Unknown database.");
            }

	}
	catch (Exception ex) {
	    throw new Exception (ex);
	}
        
        return tmpConnection;
    } /* end connectToDB method */
    /**
     *
     *
     */
//    private int getDBType (String sURL)
//    {
//        sURL.toLowerCase();
//        if (sURL.contains (":" + DB_DRIVER_ODBC + ":") == true) {
//            return DB_TYPE_ODBC;
//        }
//        return DB_TYPE_UNKNOWN;
//    }
    /**
     * 
     * @throws java.lang.Exception 
     */
    public void buildDBTree ( ) throws Exception //Connection connectionParam) throws Exception
    {
        dataBases   = null;
        try {
            vDBases = this.showDBs ();
            if (vDBases == null)
                return;
            int iDBType = super.getDBType();
            if (iDBType == DB_TYPE_ODBC) {
                vDBases.remove(Host.DB_DRIVER_ODBC);
                int iDBasesCount = vDBases.size();
                if (iDBasesCount > 0) {
                    dataBases = new DBase [iDBasesCount];
                    for (int iIdx = 0; iIdx < iDBasesCount; iIdx++) {
                        dataBases [iIdx] = new OdbcDBase (this, vDBases.get(iIdx).toString());
                    }
                }

            }
        } catch (SQLException e) {
            throw new Exception (e);
        }            
    }
    /**
     * the odbc statement "exec sp_databases" is not conformant with SQL standard
     */
    public static Vector showDBs (Connection connectionParam) throws Exception
    {
        if (connectionParam == null)
            throw new Exception ("null connection");

        Statement statement = null;
        Vector outVector = new Vector ();
        try {
            statement = connectionParam.createStatement();
            ResultSet resultSet = statement.executeQuery ("EXEC sp_databases;");
//            ResultSetMetaData rsmd = resultSet.getMetaData ();

            if (resultSet.next()) {
                do {
                    String sTmp = resultSet.getString (1); //.toLowerCase();

                    if (!outVector.contains(sTmp)) {
                        outVector.addElement (sTmp);
                    }
                } while (resultSet.next ());
            } //else {
                //outVector = null;
            //}
        }
        catch (SQLException sqlex) {
            throw new Exception ("portfolioDB::showDatabases: " + sqlex);
        }
        return outVector;
    } /* end method showDatabases */
    /**
     * 
     * @return 
     * @throws java.lang.Exception
     */
    public Vector showDBs () throws Exception
    {
        int iDBType = super.getDBType();
        if (iDBType == DB_TYPE_ODBC) {
            //System.out.println ("<<<<<<<<<<<0j0 this " + sDataSourceName + "is wrong, fix it>>>>>>>>>>>"); //
            return OdbcHost.showDBs (super.getConnection()); //connection); //sDataSourceName); //0j0 fix me
        }

        return null;
    }
} /* end class DBase */
